<?php

namespace Biblibase\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response;
use Biblibase\SiteBundle\Entity\Theme;
use Biblibase\SiteBundle\Entity\Classement;
use Biblibase\SiteBundle\Entity\Livre;
use Biblibase\SiteBundle\Entity\Auteur;
use Biblibase\SiteBundle\Entity\Editeur;
use Biblibase\SiteBundle\Entity\Collection;

use Biblibase\SiteBundle\Form\ThemeType;
use Biblibase\SiteBundle\Form\ClassementType;
use Biblibase\SiteBundle\Form\LivreType;

use Doctrine\Common\Collections\ArrayCollection;

class GestionController extends Controller
{
	public function gestionAction()
	 {
			return $this->render('BiblibaseSiteBundle:Site:gestion.html.twig');	 
	 }
	
	 public function ajoutAction()
	 {
	 		$livre = new Livre();
	 		$form = $this->createForm(new LivreType, $livre);
 
  		 	$request = $this->get('request');
  		 	if ($request->getMethod() == 'POST') {
    			$form->bind($request);
 
    		 	if ($form->isValid()) {
    		 			
    		 		$em = $this->getDoctrine()->getManager();
    		 		
    		 		$auteurRep = $em->getRepository('BiblibaseSiteBundle:Auteur');
    		 		$editeurRep = $em->getRepository('BiblibaseSiteBundle:Editeur');
    		 		$collectionRep = $em->getRepository('BiblibaseSiteBundle:Collection');
    		 		
    		 		//\si il existe déjà on remplace par celui qui existe déjà
    		 		$exist = $collectionRep->findOneBy(array('nom' => $livre->getCollection()->getNom()));
    		 		if($exist)
    		 		{
						  $livre->setCollection($exist);  		 		
    		 		}
    		 		
    		 		foreach($livre->getAuteur() as $aut)
    		 		{
    		 			$exist = $auteurRep->findOneBy(array('nom' => $aut->getNom()));
    		 			if($exist)
    		 			{
    		 					$livre->removeAuteur($aut);
							   $livre->addAuteur($exist); 		 		
    		 			}
    		 		}
    		 		
    		 		$exist = $editeurRep->findOneBy(array('nom' => $livre->getEditeur()->getNom()));
    		 		if($exist)
    		 		{
						  $livre->setEditeur($exist);  		 		
    		 		}
    		 		
      			$em->persist($livre);
      			$em->flush();
 
      			$return=array("error"=>"false", "message"=>"Livre créé");
		 			$return=json_encode($return);
		 			return new Response($return,200,array('Content-Type'=>'application/json'));
    			}
  		  }
 
  		return $this->render('::form.html.twig', array(
    		'form' => $form->createView(),
    		'route' => $this->generateUrl('biblibase_site_ajout')
  		));
	 }

	 public function ajoutThemeAction()
	 {
		$theme = new Theme();
  		$form = $this->createForm(new ThemeType, $theme);
 
  		 $request = $this->get('request');
  		 if ($request->getMethod() == 'POST') {
    		$form->bind($request);
 
    		 	if ($form->isValid()) {
      			$em = $this->getDoctrine()->getManager();
      			$em->persist($theme);
      			$em->flush();
 
      			$return=array("error"=>"false", "message"=>"Thème créé");
		 			$return=json_encode($return);
		 			return new Response($return,200,array('Content-Type'=>'application/json'));
    			}
  		  }
 
  		return $this->render('::form.html.twig', array(
    		'form' => $form->createView(),
    		'route' => $this->generateUrl('biblibase_site_ajoutTheme')
  		));
	 	/*$request = $this->getRequest();
	 	$nom = $request->request->get('theme');
	 	
	 	$em = $this->getDoctrine()->getManager();
	 	$repository = $em->getRepository('BiblibaseSiteBundle:Theme');
	 	$theme2 = $repository->findBy(array('nom' => $nom));
	 	
	 	if(count($theme2) == 0)
	 	{
	 		$theme->setNom($nom);
	 		$em->persist($theme);
	 		$em->flush();
	 		
	 		$return=array("error"=>"false", "message"=>"Thème créé");
		 	$return=json_encode($return);
		 	return new Response($return,200,array('Content-Type'=>'application/json'));
	 	}
	 	else 
	 	{
	 		$return=array("error"=>"true", "message"=>"Ce thème existe déjà");
		 	$return=json_encode($return);
		 	return new Response($return,200,array('Content-Type'=>'application/json'));
	 	}*/
	 }

	 public function ajoutClassementAction()
	 {
		$classement = new Classement();
  		$form = $this->createForm(new ClassementType, $classement);
 
  		 $request = $this->get('request');
  		 if ($request->getMethod() == 'POST') {
    		$form->bind($request);
 
    		 	if ($form->isValid()) {
      			$em = $this->getDoctrine()->getManager();
      			$em->persist($classement);
      			$em->flush();
 
      			$return=array("error"=>"false", "message"=>"Classement créé");
		 			$return=json_encode($return);
		 			return new Response($return,200,array('Content-Type'=>'application/json'));
    			}
  		  }
 
  		return $this->render('::form.html.twig', array(
    		'form' => $form->createView(),
    		'route' => $this->generateUrl('biblibase_site_ajoutClassement')
  		));
	 }

	 public function themeAction()
	 {
	 		$em = $this->getDoctrine()->getManager();
	 		$repository = $em->getRepository('BiblibaseSiteBundle:Theme');
	 		$themes = $repository->findAll();
	 		$i = 0;
	 		foreach($themes as $theme)
	 		{
	 			$theme2[$i] = array("nom"=>$theme->getNom());
	 			$i++;
	 		}
		 	$theme2=json_encode($theme2);
		 	return new Response($theme2,200,array('Content-Type'=>'application/json'));
	 }

	 public function classementAction()
	 {
	 		$em = $this->getDoctrine()->getManager();
	 		$repository = $em->getRepository('BiblibaseSiteBundle:Classement');
	 		$classements = $repository->findAll();
	 		$i = 0;
	 		foreach($classements as $classement)
	 		{
	 			$classement2[$i] = array("nom"=>$classement->getNom());
	 			$i++;
	 		}
	 		
		 	$classement2=json_encode($classement2);
		 	return new Response($classement2,200,array('Content-Type'=>'application/json'));
	 }
	 
	 public function modificationAction()
	 {
	 
	 }

	 public function suppressionAction()
	 {
	 
	 }

	 private function returnError($msg)
	 {
			$return=array("error"=>"true", "message"=>$msg);
		 	$return=json_encode($return);
		 	return new Response($return,200,array('Content-Type'=>'application/json'));	 
	 }

	 private function returnSuccess($msg)
	 {
			$return=array("error"=>"false", "message"=>$msg);
		 	$return=json_encode($return);
		 	return new Response($return,200,array('Content-Type'=>'application/json'));	 
	 }
}